/*
 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_i2c.h"
#include "iot_i2c_ex.h"

#include "bmp280.h"
#include "qma7981.h"
#include "lvgl.h"
#include "kv_store.h"


#define IOT_GPIO_FUNC_GPIO_0_I2C1_SDA  6
#define IOT_GPIO_FUNC_GPIO_1_I2C1_SCL 6
#define IOT_GPIO_FUNC_GPIO_8_GPIO 0
#define IOT_GPIO_FUNC_GPIO_14_GPIO 4
#define IOT_I2C_IDX_1 1


BMP280_HandleTypedef bmp280;
float pressure, temperature, asl;
extern uint8_t day_change;
char step_keyvalue[128] = {0};
void app_sensor_task(void)
{
    uint32_t   step;
    static char pressure_show[12];
    static char asl_show[12];
    static char step_show[12];
    char key[] = "step";


    IoTGpioInit(8);
    IoTGpioSetFunc(8,IOT_GPIO_FUNC_GPIO_8_GPIO);
    IoTGpioSetDir(8, IOT_GPIO_DIR_OUT);//设置GPIO_8为输出模式
    
    IoTGpioInit(14);
    IoTGpioSetFunc(14,IOT_GPIO_FUNC_GPIO_14_GPIO);
    IoTGpioSetDir(14, IOT_GPIO_DIR_OUT);//设置GPIO_14为输出模式
    
    IoTGpioInit(0);
    IoTGpioSetFunc(0, IOT_GPIO_FUNC_GPIO_0_I2C1_SDA);   // GPIO_0复用为I2C1_SDA
    IoTGpioInit(1);
    IoTGpioSetFunc(1, IOT_GPIO_FUNC_GPIO_1_I2C1_SCL);   // GPIO_1复用为I2C1_SCL
    IoTI2cInit(IOT_I2C_IDX_1, 400000); /* baudrate: 400kbps */

    bmp280_init_default_params(&bmp280.params);
    	while (!bmp280_init(&bmp280, &bmp280.params)) {
		// printf("BMP280 initialization failed\n");
	}
    qma7981_init();//加速度初始化
    qma7981_set_range(QMA7981_RANGE_4G);

    lv_obj_t* src = lv_scr_act();
    static lv_style_t style;
    lv_style_copy(&style, &lv_style_transp);

    lv_obj_t* label_asl = lv_label_create(src,NULL);
    lv_obj_set_pos(label_asl,30,218);
    lv_label_set_style(label_asl, 0, &style);

    lv_obj_t* label_pressure= lv_label_create(src,NULL);
    lv_obj_set_pos(label_pressure,160,218);
    lv_label_set_style(label_pressure, 0, &style);

    lv_obj_t* label_step= lv_label_create(src,NULL);
    lv_obj_set_pos(label_step,195,167);
    lv_label_set_style(label_step, 0, &style);

    while (1)
    {
        
        while (!bmp280_read_float(&bmp280, &temperature, &pressure, &asl)) {
			printf("Temperature/pressure reading failed\n");

		}
        //显示气压海拔
        sprintf(pressure_show,"%d hPa", (int)pressure);
        lv_label_set_static_text(label_pressure,pressure_show);
        sprintf(asl_show,"%d m", (int)asl);
        lv_label_set_static_text(label_asl,asl_show); 
         
        step = qma7981_read_stepcounter()+atoi(step_keyvalue);//获取步数
        sprintf(step_show,"%d", (int)step);
        UtilsSetValue(key,step_show);
  
        lv_label_set_static_text(label_step,step_show);  
        printf("step: %d \n",step);
        printf("Pressure: %.2f hPa, Temperature: %.2f C, asl: %.2f m\n",pressure, temperature,asl);
        

        usleep(1000000);
    }


    
}

